//
//  UserCenterTextField.m
//  Civacrm
//
//  Created by 王桂敏 on 2017/2/7.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "UserCenterTextField.h"

@implementation UserCenterTextField
- (BOOL)isPhone{
    if (!_isPhone) {
        self.isPhone = NO;
    }
    return _isPhone;
}

- (UserCenterTextField *)initWithFrame:(CGRect)frame
                               image:(NSString *)imageName
                         placeholder:(NSString *)placeholder
                        keyboardType:(UIKeyboardType)keyboardType
                     clearButtonMode:(UITextFieldViewMode)clearButtonMode
                            delegate:(id<UITextFieldDelegate>)delegate
           userCenterTextFieldDelegate:(id<UserCenterTextFieldDelegate>)userCenterTextFieldDelegate
{
    
    self = [super initWithFrame:frame];
    if (self) {
        //  设置输入框的基本设置
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = h(5);
        self.backgroundColor = [UIColor whiteColor];
        self.keyboardType = keyboardType;
        self.clearButtonMode = clearButtonMode;
        self.borderStyle = UITextBorderStyleNone;
        self.textAlignment = NSTextAlignmentLeft;
        
        self.delegate = delegate;
        self.userCenterTextFieldDelegate = userCenterTextFieldDelegate;
        
        self.placeholder = placeholder;
        //  设置输入框的占位图片
        if (imageName) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
            imageView.frame = CGRectMake(0, 0,kWidth(37), CGRectGetHeight(self.frame));
            self.leftView = imageView;
            
        } else {
            self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w(30), w(15))];
        }
        
        self.leftViewMode = UITextFieldViewModeAlways;
        self.font = font(32);
        self.textColor = ColorWithHexString(@"719da5");
        [self setValue:ColorWithHexString(@"bad9df") forKeyPath:@"_placeholderLabel.textColor"];
        [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        self.returnKeyType = UIReturnKeyDone;
    }
    
    return self;
}

- (void)textFieldDidChange:(UITextField *)textField{
    NSString *toBeString = textField.text;
    BOOL isEmoj = [self stringContainsEmoji:toBeString];
    toBeString = [self disable_emoji:toBeString];
    if (isEmoj) {
        textField.text = toBeString;
    }
    if (self.userCenterTextFieldDelegate && [self.userCenterTextFieldDelegate respondsToSelector:@selector(textFieldDidChange:)]) {
        [self.userCenterTextFieldDelegate textFieldDidChange:self];
    }
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        
        self.rightButton = [UIButton buttonOfImageForNormalWithImage:nil frame:CGRectMake(0, 0, w(150), self.height) target:self action:nil];
        _rightButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.rightView = _rightButton;
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    return _rightButton;
}

- (void)setRightViewWithNormalImage:(NSString *)normalImage
                  selectedImageView:(NSString *)selectedImageView{
    [self.rightButton setImage:Image(normalImage) forState:UIControlStateNormal];
    [self.rightButton setImage:Image(selectedImageView) forState:UIControlStateSelected];
}










/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
